package com.example.blew.omp.system.controller;


import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.NumberUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.builder.ExcelReaderBuilder;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.example.blew.demo.uitl.DemoDataListener;
import com.example.blew.omp.system.model.ReturnModel;
import com.example.blew.omp.system.model.form.ExcelData;
import com.example.blew.omp.system.model.form.FileForm;
import com.example.blew.omp.system.service.FileService;
import io.swagger.annotations.Api;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("file")
@Api(tags = "FileController", value = "文件上传")
public class FileController {

    @Resource
    private FileService fileService;


    @Operation(summary = "上传多个文件", description = "上传多个文件")
    @PostMapping(value = "upload", consumes = {MediaType.MULTIPART_FORM_DATA_VALUE})
    public ReturnModel<List<Long>> upload(MultipartFile[] files){
        List<Long> ids=new ArrayList<>();
        ArrayList<MultipartFile> multipartFiles = CollUtil.newArrayList(files);
        List<FileForm> fileList = multipartFiles.stream().map(file -> {
            Long id = IdWorker.getId();
            ids.add(id);
            String fileName = file.getOriginalFilename();
            String fileType = FileUtil.getSuffix(fileName);
            String storePath = fileService.upload(file, fileName);
            return FileForm.builder()
                    .id(id).fileName(fileName)
                    .fileType(fileType).storePath(storePath)
                    .fileSize(NumberUtil.div(String.valueOf(file.getSize()), String.valueOf(1024)))
                    .build();
        }).collect(Collectors.toList());
        fileService.save(fileList);
        return ReturnModel.newSuccessInstance(ids);
    }


    @Operation(summary = "上传多个文件", description = "上传多个文件")
    @PostMapping(value = "uploadExcel", consumes = {MediaType.MULTIPART_FORM_DATA_VALUE})
    public ReturnModel<Boolean> uploadExcel(MultipartFile file) throws IOException {
//        DemoDataListener demoDataListener = new DemoDataListener();
//        ExcelReaderBuilder read = EasyExcel.read(file.getInputStream(), ExcelData.class, demoDataListener);
//        read.sheet().doRead();
//        return ReturnModel.newSuccessInstance(true);

//        (dataList -> {
//            for (ExcelData demoData : dataList) {
//                log.info("读取到一条数据{}", JSON.toJSONString(demoData));
//            }
//        }))
        EasyExcel.read(file.getInputStream(), ExcelData.class, new DemoDataListener()).sheet().doRead();
        return ReturnModel.newSuccessInstance(true);
    }



}
